package com.hcy.rabbitbasicmodel.model;

import com.hcy.rabbitbasicmodel.enums.Code;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author huchenying
 * @Description
 * @Date 2021/11/17
 **/
@Data
public class ReturnResult implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private Object data;

    public static ReturnResult success() {
        ReturnResult result = new ReturnResult();
        result.setCode(Code.Success);
        result.setMsg("请求成功");
        return result;
    }

    public static ReturnResult success(String message) {
        ReturnResult result = new ReturnResult();
        result.setCode(Code.Success);
        result.setMsg(message);
        return result;
    }

    public static ReturnResult success(Object data) {
        ReturnResult result = new ReturnResult();
        result.setCode(Code.Success);
        result.setData(data);
        result.setMsg("请求成功");
        return result;
    }

    public static ReturnResult fail(String message) {
        ReturnResult result = new ReturnResult();
        result.setCode(Code.Fail);
        result.setMsg(message);
        return result;
    }

    public static ReturnResult exception(String message) {
        ReturnResult result = new ReturnResult();
        result.setCode(Code.Exception);
        result.setMsg(message);
        return result;
    }

    public void setCode(Code code) {
        this.code = code.getCode();
    }
    public void setCode(int code) {
        this.code = code;
    }

    public ReturnResult setData(Object data) {
        this.data = data;
        return this;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    public boolean isSuccess() {
        return this.getCode().equals(Code.Success.getCode());
    }
}
